package com.gy.zpchat.ui.friend.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import im.zego.zim.entity.ZIMUserInfo;
import com.gy.zpchat.R;

public class BlacklistListAdapter extends RecyclerView.Adapter {

    private OnItemLongClickListener onItemLongClickListener;
    private final List<ZIMUserInfo> userInfoList = new ArrayList<>();

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_blacklist_content, parent, false);
        return new ZIMFriendViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ZIMFriendViewHolder) {
            final ZIMFriendViewHolder zimFriendViewHolder = (ZIMFriendViewHolder)holder;

            ZIMUserInfo zimUserInfo = userInfoList.get(position);
            zimFriendViewHolder.setZIMFriendApplicationInfo(zimUserInfo);

            if (onItemLongClickListener != null) {
                zimFriendViewHolder.setOnItemLongClickListener(onItemLongClickListener);
            }
        }
    }

    @Override
    public int getItemCount() {
        return userInfoList.size();
    }

    public void setOnItemLongClickListener(OnItemLongClickListener mOnItemLongClickListener) {
        this.onItemLongClickListener = mOnItemLongClickListener;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void addAll(ArrayList<ZIMUserInfo> zimUserInfos) {
       this.userInfoList.clear();
        this.userInfoList.addAll(zimUserInfos);
        notifyDataSetChanged();
    }



    public interface OnItemLongClickListener {
        void onItemLongClick(ZIMUserInfo userInfo);

    }

    public static class ZIMFriendViewHolder extends RecyclerView.ViewHolder {
        TextView userNameTextView;
        ImageView avatarImageView;
        RelativeLayout list;

        public ZIMFriendViewHolder(View itemView) {
            super(itemView);
            list = itemView.findViewById(R.id.list);
            userNameTextView = itemView.findViewById(R.id.user_name);
            avatarImageView = itemView.findViewById(R.id.avatar);

        }

        public OnItemLongClickListener listener;

        public void setZIMFriendApplicationInfo(ZIMUserInfo userInfo) {
            String userName = userInfo.userName;
            if (!"".equals(userName)) {
                userNameTextView.setText(userName);
            } else if (!"".equals(userInfo.userID)) {
                userNameTextView.setText(userInfo.userID);
            }

            Glide.with(this.avatarImageView.getContext())
                .load(userInfo.userAvatarUrl)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                .into(avatarImageView);

            list.setOnLongClickListener(new View.OnLongClickListener(){

                @Override
                public boolean onLongClick(View v) {
                    if(listener!=null){
                        listener.onItemLongClick(userInfo);
                    }
                    return false;
                }
            });
        }

        public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
            listener = onItemLongClickListener;
        }
    }
}
